package com.nengchuan.kbms.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nengchuan.kbms.dfs.annotation.Relation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Collection;

/**
 * <b>Description:</b><br>
 * 自动条件包装类
 * @author Abe
 * @version 1.0
 * <br><b>Date:</b> 2019/7/8 18:04
 */
public class AutoWrapper<T> extends QueryWrapper<T> {

    private Object object;

    public AutoWrapper(Object object) {
        super();
        this.object = object;
        init();
    }

    private void init() {
        if (object == null) {
            return;
        }
        Field[] fields = object.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                Relation relation = field.getAnnotation(Relation.class);
                if (relation != null) {
                    field.setAccessible(true);
                    Object value = field.get(object);
                    if (!StringUtils.isEmpty(value)) {
                        Relation.Bind bind = relation.value();
                        String fieldName = relation.field();
                        if (StringUtils.isEmpty(fieldName)) {
                            fieldName = camel2Underline(field.getName());
                        }
                        if (Relation.Bind.EQ == bind) {
                            eq(fieldName, value);
                        } else if (Relation.Bind.LIKE == bind) {
                            like(fieldName, value);
                        } else if (Relation.Bind.GT == bind) {
                            gt(fieldName, value);
                        } else if (Relation.Bind.GE == bind) {
                            ge(fieldName, value);
                        } else if (Relation.Bind.LT == bind) {
                            lt(fieldName, value);
                        } else if (Relation.Bind.LE == bind) {
                            le(fieldName, value);
                        } else if (Relation.Bind.IN == bind) {
                            if (value instanceof Collection) {
                                if (CollectionUtils.isNotEmpty((Collection<?>) value)) {
                                    in(true, fieldName, (Collection<?>) value);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * <b>Description:</b><br>
     * 驼峰命名转下划线
     * @param fieldName 驼峰命名
     * @return
     * <b>Author:</b> abeholder
     */
    public static String camel2Underline(String fieldName) {
        if (fieldName != null && fieldName.length() > 0) {
            StringBuilder sb = new StringBuilder(fieldName.length() + 4);
            char[] chars = fieldName.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (Character.isUpperCase(chars[i])) {
                    sb.append('_').append(Character.toLowerCase(chars[i]));
                } else {
                    sb.append(chars[i]);
                }
            }
            return sb.toString();
        }
        return fieldName;
    }

    /**
     * 自定义 and 条件，xml中使用 ~
     * @author abe
     * @return
     */
    public String getSqlHolder() {
        String sql = "";
        String sqlSegment = getSqlSegment();
        if (!StringUtils.isEmpty(sqlSegment)) {
            sql = "and " + sqlSegment;
        }
        return sql;
    }

//    @Override
//    public String getCustomSqlSegment() {
//        return super.getCustomSqlSegment().replaceAll("ew.", "wrapper.ew.");
//    }

}
